
package com.cms.recursoshumanos.component;

import com.cms.recursoshumanos.component.base.LabelListMenuItem;
import com.cms.recursoshumanos.component.base.ListStyleButtonField2;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.object.SolicitudCompra;
import com.cms.recursoshumanos.utilities.Tool;

import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.Touchscreen;
import net.rim.device.api.ui.Ui;

public class SolicitudCompraListButtonField extends ListStyleButtonField2
{
	private static final int WIDTH2 = Display.getWidth();
	private int WIDTH_TIME = 126;
	public static int DRAWPOSITION_TOP = 0;
	public static int DRAWPOSITION_BOTTOM = 1;
	public static int DRAWPOSITION_MIDDLE = 2;
	public static int DRAWPOSITION_SINGLE = 3;
	
	protected static final int CORNER_RADIUS = 18;
	
	private static final int HPADDING = (WIDTH2 <= 320) ? 6 : 8;
	private static final int VPADDING = 2;
	
	private static final int COLOR_BACKGROUND = 0xFFFFFF;
	protected static final int COLOR_BORDER = 0xBBBBBB;
	protected static int COLOR_BACKGROUND_FOCUS = 0x186DEF;
	
	private LabelListMenuItem _labelField;
	private LabelListMenuItem _descripcionField;
	private LabelListMenuItem _tipoField;
	
	private SolicitudCompra laCompra;
	
	private int _targetHeight;
	private int _rightOffset;
	private int _leftOffset;
	private int _labelHeight;
	private int _descripcionHeight;
	
	protected int _drawPosition = -1;
	

	public SolicitudCompraListButtonField( SolicitudCompra laCompra, long style )
	{
	    super( USE_ALL_WIDTH | Field.FOCUSABLE );
		this.laCompra = laCompra;
	    _labelField = new LabelListMenuItem( "Nro. "+laCompra.getSolicitudCompraId(), style );
		_descripcionField = new LabelListMenuItem( "Fecha: "+ 
													Tool.getInstance().parseDateToString(laCompra.getFechaSolicitudCompra(),2), 
													style );
		
		_tipoField = new LabelListMenuItem( laCompra.getTipoStatusSolicitudCompra().getNombreTipoStatusSol(), 0 );
		
		 
		Font defaultFont = getFont();
		Font font2 = defaultFont.derive(Font.ITALIC, 5, Ui.UNITS_pt);
		Font font = defaultFont.derive(Font.PLAIN, 8, Ui.UNITS_pt);
		Font font3 = defaultFont.derive(Font.BOLD | Font.CURSIVE_STYLE, 6, Ui.UNITS_pt);
		
		_labelField.setFont(font);
		_descripcionField.setFont(font2);
		_tipoField.setFont(font3);
		
		
	}
	
	
	public SolicitudCompraListButtonField(long l) {
		  super(l);
	}
	
	/**
	 * DRAWPOSITION_TOP | DRAWPOSITION_BOTTOM | DRAWPOSITION_MIDDLE
	 * Determins how the field is drawn (borders)
	 * If none is set, then no borders are drawn
	 */
	public void setDrawPosition( int drawPosition )
	{
	    _drawPosition = drawPosition;
	}
	
	public String toString()
	{
	    return _labelField.toString()+", "+_descripcionField.toString()+", "+_tipoField.toString();
	}
	
	public void layout( int width, int height )
	{
	
	    _targetHeight = getFont().getHeight() / 2 * 3 + 2 * VPADDING;
	//#ifndef VER_4.6.1 | VER_4.6.0 | VER_4.5.0 | VER_4.2.1 | VER_4.2.0
	    if( Touchscreen.isSupported() ) {
	        _targetHeight = getFont().getHeight() * 2 + 2 * VPADDING;
	    }
	//#endif
		
	    _targetHeight *= 1.5;
	    
	    _leftOffset = HPADDING;
	    _rightOffset = HPADDING;
	    
	    WIDTH_TIME = _tipoField.getFont().getAdvance(_tipoField.getText());
	    _tipoField.layout(WIDTH_TIME, height/2); // width - _leftOffset - _rightOffset - labelWidth - 10
	    int datetimeWidth = _tipoField.getWidth();
	    
	    _labelField.layout( width - _leftOffset - _rightOffset - datetimeWidth - 10, height/2 );
	    _labelHeight = _labelField.getHeight();
	    int labelWidth = _labelField.getWidth(); //getFont().getAdvance(_labelField.getText());
	    
	    _descripcionField.layout( width - _leftOffset - _rightOffset, height/2 );
	    _descripcionHeight = _descripcionField.getFont().getHeight();
	    int locationWidth = _descripcionField.getWidth();
	    
	    if( _labelField.isStyle( DrawStyle.HCENTER ) ) {
	        _leftOffset = ( width - Math.max(labelWidth+datetimeWidth,locationWidth) ) / 2;
	    } else if ( _labelField.isStyle( DrawStyle.RIGHT ) ) {
	        _leftOffset = width - Math.max(labelWidth+datetimeWidth,locationWidth) - HPADDING - _rightOffset;
	    }
	    
	    int extraVPaddingNeeded = 0;
	    if( (_labelHeight + _descripcionHeight) < _targetHeight ) {
	        // Make sure that they are at least 1.5 times font height
	        extraVPaddingNeeded =  ( _targetHeight - _labelHeight - _descripcionHeight ) / 2;
	    }
	    
	    setExtent( width, _labelHeight + _descripcionHeight + 20 + 2 * extraVPaddingNeeded );
	}
	
	protected void paint( Graphics g )
	{
		  int color = g.getColor();
		  
		  // Left Bitmap
	    
	    int labelWidth = _labelField.getWidth();
	    int datetimeWidth = _tipoField.getWidth();
	    // Label
	    try {
	        g.pushRegion( _leftOffset, ( getHeight() - (_labelHeight + _descripcionHeight + 10) ) / 2, getWidth() - _leftOffset - _rightOffset - datetimeWidth, _labelHeight, 0, 0 );
	        g.setFont(_labelField.getFont());
	        _labelField.paint( g );
	    } finally {
	        g.popContext();
	    }
	    // Location
		  g.setColor(Color.DARKSLATEGRAY);
	    try {
	        g.pushRegion( _leftOffset+10, (( getHeight() - (_labelHeight + _descripcionHeight + 10) ) / 2) + _labelHeight + (_tipoField.getHeight()/2), getWidth() - _leftOffset - _rightOffset, _descripcionHeight, 0, 0 );
	        g.setFont(_descripcionField.getFont());
	        _descripcionField.paint( g );
	    } finally {
	        g.popContext();
	    }
	    // Datetime
	    int colorFocus = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? Color.WHITE : Color.DARKGRAY;
	    g.setColor(colorFocus);
	    try {
	        g.pushRegion( 5 + getWidth() - _rightOffset - datetimeWidth,  (_tipoField.getHeight()/2) + 10, getWidth() - _leftOffset - _rightOffset - labelWidth, _tipoField.getHeight(), 0, 0 );
	        g.setFont(_tipoField.getFont());
	        _tipoField.paint( g );
	    } finally {
	        g.popContext();
	    }
	    g.setColor(color);
	    // Right (Action) Bitmap
	  
	}
	
	protected void paintBackground( Graphics g )
	{
	    if( _drawPosition < 0 ) {
	        // it's like a list field, let the default background be drawn
	        super.paintBackground( g );
	        return;
	    }
	    
	    int oldColour = g.getColor();
	    
	    int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? COLOR_BACKGROUND_FOCUS : COLOR_BACKGROUND;
	    try {
	        if( _drawPosition == 0 ) {
	            // Top
	            g.setColor( background );
	            g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
	            g.setColor( COLOR_BORDER );
	            g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
	            g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
	        } else if( _drawPosition == 1 ) {
	            // Bottom 
	            g.setColor( background );
	            g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
	            g.setColor( COLOR_BORDER );
	            g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
	        } else if( _drawPosition == 2 ) {
	            // Middle
	            g.setColor( background );
	            g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
	            g.setColor( COLOR_BORDER );
	            g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
	            g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
	        } else {
	            // Single
	            g.setColor( background );
	            g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
	            g.setColor( COLOR_BORDER );
	            g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
	        }
	    } finally {
	        g.setColor( oldColour );
	    }
	}
	
	protected void drawFocus( Graphics g, boolean on )
	{
	    if( _drawPosition < 0 ) {
	        super.drawFocus( g, on );
	    } else {
	        boolean oldDrawStyleFocus = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS );
	        try {
	            if( on ) {
	                g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
	            }
	            paintBackground( g );
	            paint( g );
	        } finally {
	            g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus );
	        }
	    }
	}
	
	protected boolean keyChar( char character, int status, int time ) 
	{
	    if( character == Characters.ENTER ) {
	        clickButton();
	        return true;
	    }
	    return super.keyChar( character, status, time );
	}
	
	protected boolean navigationClick( int status, int time ) 
	{
	    clickButton(); 
	    return true;    
	}
	
	protected boolean trackwheelClick( int status, int time )
	{        
	    clickButton();    
	    return true;
	}
	
	//#ifndef VER_4.1.0 | 4.0.0
	    protected boolean invokeAction( int action ) 
	    {
	        switch( action ) {
	            case ACTION_INVOKE: {
	                clickButton(); 
	                return true;
	            }
	        }
	        return super.invokeAction( action );
	    }
	//#endif        
	     
	/**
	 * A public way to click this button
	 */
	public void clickButton() 
	{
	    fieldChangeNotify( 0 );
	}
	   
	//#ifndef VER_4.6.1 | VER_4.6.0 | VER_4.5.0 | VER_4.2.1 | VER_4.2.0
	protected boolean touchEvent( TouchEvent message )
	{
	    int x = message.getX( 1 );
	    int y = message.getY( 1 );
	    if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
	        // Outside the field
	        return false;
	    }
	    switch( message.getEvent() ) {
	   
	        case TouchEvent.UNCLICK:
	            clickButton();
	            return true;
	    }
	    return super.touchEvent( message );
	}
	//#endif 
	
	public void setDirty( boolean dirty ) {}
	public void setMuddy( boolean muddy ) {}
	
	
	
}
